#include "../include/com_foxit_pdf_dao_PDF2PDF.h"
#include "../include/import.h"
#include "../include/FXAPI.h"
#include <stdio.h>

#include <iostream>
using namespace std;

jlong JNICALL Java_com_foxit_pdf_dao_PDF2PDF_FP2PCreateModuleMgr
  (JNIEnv *Env, jclass cls)
{
	return (jlong)FP2P_CreateModuleMgr();
}
void JNICALL Java_com_foxit_pdf_dao_PDF2PDF_FP2PDestroyModuleMgr
  (JNIEnv *Env, jclass cls, jlong mgr)
{
	PDF2PDFMoudleMgr pMgr = (PDF2PDFMoudleMgr)mgr;
	FP2P_DestroyModuleMgr(pMgr);
}
 
jlong JNICALL Java_com_foxit_pdf_dao_PDF2PDF_FP2PNewProcessor
  (JNIEnv *Env, jclass cls)
{
	return (jlong)FP2P_NewProcessor();
}

void JNICALL Java_com_foxit_pdf_dao_PDF2PDF_FP2PCloseProcessor
  (JNIEnv *Env, jclass cls, jlong proccessor)
{
	PDF2PDFProcessor p = (PDF2PDFProcessor)proccessor;
	FP2P_CloseProcessor(p);

}


jint JNICALL Java_com_foxit_pdf_dao_PDF2PDF_FP2PStartExtract
  (JNIEnv *Env, jclass cls, jlong proccessor, jbyteArray file, jbyteArray pwd)
{

	jint rst = -1;
	jsize len = Env->GetArrayLength(file);
	jboolean isCopy = 0;
	jbyte *pBuf = Env->GetByteArrayElements(file, &isCopy);
char* pp = (char*)pBuf;

	CFX_WideString wstrFilePath = CFX_WideString::FromUTF8((const char*)pBuf, len);
	Env->ReleaseByteArrayElements(file, pBuf, JNI_ABORT);

	len = Env->GetArrayLength(pwd);
	pBuf = Env->GetByteArrayElements(pwd, &isCopy);
	CFX_ByteString bstrPassword((FX_LPSTR)pBuf, len);
	Env->ReleaseByteArrayElements(pwd, pBuf, JNI_ABORT);

	PDF2PDFProcessor p = (PDF2PDFProcessor)proccessor;
	int n = FP2P_StartExtract(p, (wchar_t*)FX_LPCWSTR(wstrFilePath), (const char*)FX_LPCSTR(bstrPassword));
	
	return n;
}


jint JNICALL Java_com_foxit_pdf_dao_PDF2PDF_FP2PCountPage
  (JNIEnv *Env, jclass cls, jlong proccessor)
{

	PDF2PDFProcessor p = (PDF2PDFProcessor)proccessor;
	return FP2P_CountPage(p);
}


jint JNICALL Java_com_foxit_pdf_dao_PDF2PDF_FP2PCountParagraph
  (JNIEnv *Env, jclass cls, jlong proccessor, jint iPage)
{

	PDF2PDFProcessor p = (PDF2PDFProcessor)proccessor;
	return FP2P_CountParagraph(p, iPage);
}
jboolean JNICALL Java_com_foxit_pdf_dao_PDF2PDF_FP2PIsAllLineIsIndepend
  (JNIEnv *Env, jclass cls, jlong proccessor, jint iPage, jint iPara)
{
	PDF2PDFProcessor p = (PDF2PDFProcessor)proccessor;
	if(!FP2P_IsAllLineIsIndepend(p, iPage, iPara)) return false;
	return true;
}

jint JNICALL Java_com_foxit_pdf_dao_PDF2PDF_FP2PCountIndependLineInParagraph
  (JNIEnv *Env, jclass cls, jlong proccessor, jint iPage, jint iPara)
{
	PDF2PDFProcessor p = (PDF2PDFProcessor)proccessor;
	return FP2P_CountIndependLineInParagraph(p, iPage, iPara);
}


jlong JNICALL Java_com_foxit_pdf_dao_PDF2PDF_FP2PGetIndependLineContent
  (JNIEnv *Env, jclass cls, jlong proccessor, jint iPage, jint iPara, jint iLine)
{
	PDF2PDFProcessor p = (PDF2PDFProcessor)proccessor;
	return (jlong)FP2P_GetIndependLineContent(p, iPage, iPara, iLine);
}

jlong JNICALL Java_com_foxit_pdf_dao_PDF2PDF_FP2PGetParagraphContent
  (JNIEnv *Env, jclass cls, jlong proccessor, jint iPage, jint iPara)
{

	PDF2PDFProcessor p = (PDF2PDFProcessor)proccessor;
	return (jlong)FP2P_GetParagraphContent(p, iPage, iPara);
}

jstring JNICALL Java_com_foxit_pdf_dao_PDF2PDF_FP2PGetContent
  (JNIEnv *Env, jclass cls, jlong content)
{

	PDFWStringContent c = (PDFWStringContent)content;
	CFX_WideString* pWs = (CFX_WideString*)c;
	CFX_ByteString bs = pWs->UTF8Encode();
	jstring jstr = Env->NewStringUTF((FX_LPCSTR)bs);

jsize len = Env->GetStringUTFLength(jstr);


jboolean isCopy = 0;
const char* dest = Env->GetStringUTFChars(jstr, &isCopy);

	return jstr;

}

 void JNICALL Java_com_foxit_pdf_dao_PDF2PDF_FP2PFreeContent
  (JNIEnv *Env, jclass cls, jlong content)
 {

	 PDFWStringContent c = (PDFWStringContent)content;
	 FP2P_FreeContent(c);
 }

void JNICALL Java_com_foxit_pdf_dao_PDF2PDF_FP2PInitDestInfo
  (JNIEnv *Env, jclass cls, jlong proccessor, jint charset)
{
	PDF2PDFProcessor p = (PDF2PDFProcessor)proccessor;
	FP2P_InitDestInfo(p, charset);
}

void JNICALL Java_com_foxit_pdf_dao_PDF2PDF_FP2PAddParagraph
  (JNIEnv *Env, jclass cls, jlong proccessor, jint iPage, jint iPara, jbyteArray content)
{

	PDF2PDFProcessor p = (PDF2PDFProcessor)proccessor;
	jsize len = Env->GetArrayLength(content);
	jboolean isCopy = 0;
	jbyte *pBuf = Env->GetByteArrayElements(content, &isCopy);
char* pp = (char*)pBuf;

	CFX_WideString wstrCon = CFX_WideString::FromUTF8((const char*)pBuf, len);
	Env->ReleaseByteArrayElements(content, pBuf, JNI_ABORT);
	FP2P_AddParagraph(p, iPage, iPara, (wchar_t*)(FX_LPCWSTR)wstrCon);
	
}

void JNICALL Java_com_foxit_pdf_dao_PDF2PDF_FP2PAddIndependLine
  (JNIEnv *Env, jclass cls, jlong proccessor, jint iPage, jint iPara, jint iLine, jbyteArray content)
{
	PDF2PDFProcessor p = (PDF2PDFProcessor)proccessor;
	jsize len = Env->GetArrayLength(content);
	jboolean isCopy = 0;
	jbyte *pBuf = Env->GetByteArrayElements(content, &isCopy);

	CFX_WideString wstrCon = CFX_WideString::FromUTF8((const char*)pBuf, len);
	Env->ReleaseByteArrayElements(content, pBuf, JNI_ABORT);
	FP2P_AddIndependLine(p, iPage, iPara, iLine, (wchar_t*)(FX_LPCWSTR)wstrCon);
}

jint JNICALL Java_com_foxit_pdf_dao_PDF2PDF_FP2PSaveFile
  (JNIEnv *Env, jclass cls, jlong proccessor, jbyteArray destFile)
{

	jsize len = Env->GetArrayLength(destFile);
	jboolean isCopy = 0;
	jbyte *pBuf = Env->GetByteArrayElements(destFile, &isCopy);
char* pp = (char*)pBuf;

	CFX_WideString wstrFile = CFX_WideString::FromUTF8((const char*)pBuf, len);
	Env->ReleaseByteArrayElements(destFile, pBuf, JNI_ABORT);
	PDF2PDFProcessor p = (PDF2PDFProcessor)proccessor;
	int n = FP2P_SaveFile(p, (wchar_t*)(FX_LPCWSTR)wstrFile);
	
	return n;
}
